package com.yxhl.zoume.core.coupon.presenter;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.coupon.view.CurrentCouponView;
import com.yxhl.zoume.data.http.rest.param.coupon.CurrentCouponParam;
import com.yxhl.zoume.data.http.rest.response.coupon.CurrentCouponResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.coupon.CurrentCouponUseCase;

import javax.inject.Inject;

/**
 * Created by vhawk on 2016/11/3.
 */

public class CurrentCouponPresenter extends BasePresenter {

    @Inject
    CurrentCouponUseCase useCase;

    private CurrentCouponView view;

    @Inject
    public CurrentCouponPresenter(Context context) {
        super(context);
    }

    @Override
    public void attachView(BaseView view) {
        super.attachView(view);
        this.view = (CurrentCouponView) view;
    }

    public void fetchCurrentCoupon() {
        CurrentCouponParam param = new CurrentCouponParam();
        param.setToken(getToken())
                .setMobile(getMobile());

        useCase.execute(param, new ZMSubscriber<CurrentCouponResponse>() {
            @Override
            public void onNext(CurrentCouponResponse response) {
                super.onNext(response);
                if (response.isSucc()) {
                    view.handleCurrentCouponSucceed(response.getData().getCoupons());
                } else {
                    view.handleCurrentCouponFailure(response.getResultMessage());
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.handleCurrentCouponNetError();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        useCase.unsubscribe();
    }
}
